package cn.techaction.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import cn.techaction.dao.ActionProductDao;
import cn.techaction.pojo.ActionProduct;
import cn.techaction.pojo.User;
@Repository
public class ActionProductDaoImpl implements ActionProductDao {
	@Autowired
	private QueryRunner queryRunner;
	private String alias="id,name,product_id as productId,parts_id as partsId,icon_url as iconUrl,sub_images as subImages"
			+ ",detail,spec_param as specParam,price,stock,status,is_hot as hot,created,updated";
	@Override
	public ActionProduct findProductById(Integer id) {
		// TODO Auto-generated method stub
		String sql="select "+this.alias+" from product where id = ?";
		try {
			return queryRunner.query(sql,new BeanHandler<ActionProduct>(ActionProduct.class),id);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@Override
	public List<ActionProduct> findProducts(String name) {
		// TODO Auto-generated method stub
		String sql="select * from product where name like ?";
		List<ActionProduct> goods=new ArrayList<>();
		try {
			goods=queryRunner.query(sql,new BeanListHandler<ActionProduct>(ActionProduct.class),name);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return goods;
	}
	@Override
	public List<ActionProduct> findHotProducts(Integer num) {
		// TODO Auto-generated method stub
		String sql="select "+ this.alias +" from product where is_hot=1 and status = 2 order by updated,id desc";
		if(num!=null) {
			sql+=" limit 0,?";
		}
		try {
			if(num !=null) {
				return queryRunner.query(sql, new BeanListHandler<ActionProduct>(ActionProduct.class), num);
			}else {
				return queryRunner.query(sql, new BeanListHandler<ActionProduct>(ActionProduct.class));
			}
			
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public List<ActionProduct> findProductsByProductCategory(Integer categoryId) {
		// TODO Auto-generated method stub
		String sql="select "+this.alias+" from product where product_id=? and status =2 order by updated desc";
		try {
			return queryRunner.query(sql,new BeanListHandler<ActionProduct>(ActionProduct.class),categoryId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	} 
	@Override
	public int updateProduct(ActionProduct product) {
		 String sql="update product set updated = ?";
		List<Object> params=new ArrayList<>();
		params.add(product.getUpdated());
		if (!StringUtils.isEmpty(product.getName())) {
			sql+=",name = ?";
			params.add(product.getName());
		}
		if (product.getProductId()!=null) {
			sql+=",product_id = ?";
			params.add(product.getProductId());
		}
		if (product.getPartsId()!=null) {
			sql+=",parts_id = ?";
			params.add(product.getPartsId());
		}
		if (product.getPrice()!=null) {
			sql+=",price = ?";
			params.add(product.getPrice());
		}
		if (product.getStock()!=null) {
			sql+=",stock = ?";
			params.add(product.getStock());
		}
		if (product.getStatus()!=null) {
			sql+=",status = ?";
			params.add(product.getStatus());
		}if (!StringUtils.isEmpty(product.getDetail())) {
			sql+=",detail = ?";
			params.add(product.getDetail());
		}
		if (!StringUtils.isEmpty(product.getSpecParam())) {
			sql+=",spec_param = ?";
			params.add(product.getSpecParam());
		}
		if (product.getHot()!=null) {
			sql+=",is_hot = ?";
			params.add(product.getHot());
		}
		sql+=" where id = ?";
		params.add(product.getId());
		try {
		return	queryRunner.update(sql,params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public int deleteCartProduct(Integer uid) {
		 String sql="delete from shop_car where id = ?";
		 try {
			return queryRunner.update(sql,uid);
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	
	}
	@Override
	public Integer getTotalCount(ActionProduct product) {
		// TODO Auto-generated method stub
		String sql="select count(id) as num from product where 1=1";
		List<Object> params=new ArrayList<>();
		if (product.getId()!=null) {
			sql+=" and id = ?";
			params.add(product.getId());
		}
		if (product.getName()!=null) {
			sql+=" and name like ?";
			params.add(product.getName());
		}
		if (product.getStatus()!=null) {
			sql+=" and status = ?";
			params.add(product.getStatus());
		}
		if (product.getProductId()!=null) {
			sql+=" and product_id = ?";
			params.add(product.getProductId());
		}
		if (product.getPartsId()!=null) {
			sql+=" and parts_id = ?";
			params.add(product.getPartsId());
		}
		try {
			return queryRunner.query(sql,new ColumnListHandler<Long>("num"),params.toArray()).get(0).intValue();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
		
	}
	@Override
	public List<ActionProduct> findProducts(ActionProduct product, int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		String sql="select "+this.alias+" from product where 1=1";
		List<Object> params=new ArrayList<>();
		if (product.getId()!=null) {
			sql+=" and id = ?";
			params.add(product.getId());
		}
		if (product.getName()!=null) {
			sql+=" and name like ?";
			params.add(product.getName());
		}
		if (product.getStatus()!=null) {
			sql+=" and status = ?";
			params.add(product.getStatus());
		}
		if (product.getProductId()!=null) {
			sql+=" and product_id = ?";
			params.add(product.getProductId());
		}
		if (product.getPartsId()!=null) {
			sql+=" and parts_id = ?";
			params.add(product.getPartsId());
		}
		sql+=" limit ?,?";
		params.add(startIndex);
		params.add(pageSize);
		try {
			return queryRunner.query(sql,new BeanListHandler<ActionProduct>(ActionProduct.class),params.toArray());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
	} 
}
